package com.ktjiaotu;

import java.lang.reflect.Method;

/**
 * @Author: DABINGCHNEGJIAO
 * @CreateTime: 2025-06-03
 * @Description:
 * @Version: 1.0
 */
public class AccessPersonMethods {
    public static void main(String[] args) {
        try {
            Class clz = Class.forName("com.ktjiaotu.pojo.Person");
            Method getAge = clz.getDeclaredMethod("getAge");
            getAge.setAccessible(true);
            Object invoke = getAge.invoke(null);
            System.out.println("年龄是" + invoke);
            Object person = clz.newInstance();
            Method silentMethod = clz.getDeclaredMethod("silentMethod");
            silentMethod.setAccessible(true);
            silentMethod.invoke(person);
            Method setName = clz.getDeclaredMethod("setName", String.class);
            setName.setAccessible(true);
            setName.invoke(person, "new name");
            Object getName = clz.getDeclaredMethod("getName").invoke(person);
            System.out.println("getName返回值为:" + getName);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
